package com.springboot.seckill.controller;

import com.springboot.seckill.error.BusinessException;
import com.springboot.seckill.error.EmBusinessError;
import com.springboot.seckill.response.CommonReturnType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @Author: HuangDekai
 * @Date: 2022/3/8 20:44
 * @Version: 1.0
 * @since: jdk11
 */
@RestController
public class BaseController {
    private static final Logger log = LoggerFactory.getLogger(BaseController.class);
    private static final String ERR_CODE = "errCode";
    private static final String ERR_MSG = "errMsg";

//    //定义ExceptionHandler解决未被controller层吸收的exception
//    @ExceptionHandler(Exception.class) //当抛出Exception.class时进入该处理环节
//    @ResponseStatus(HttpStatus.OK)     //返回http的状态码为200
//    public Object handlerException(HttpServletRequest request, Exception ex) {
//
//        HashMap<String, Object> responseData = new HashMap<>();
//        //如果不是BusinessException呢?
//        if (ex instanceof MethodArgumentNotValidException) {
//            responseData.put(ERR_CODE, EmBusinessError.PARAMETER_VALIDATION_ERROR.getErrCode());
//            responseData.put(ERR_MSG, ((MethodArgumentNotValidException) ex).getBindingResult().getFieldError().getDefaultMessage());
//        }
//        else if (ex instanceof BusinessException) {
//            BusinessException businessException = (BusinessException) ex;
//            responseData.put(ERR_CODE, businessException.getErrCode());
//            responseData.put(ERR_MSG, businessException.getErrMsg());
//        }
//        else {
//            responseData.put(ERR_CODE, EmBusinessError.UNKNOWN_ERROR.getErrCode());
//            responseData.put(ERR_MSG, EmBusinessError.UNKNOWN_ERROR.getErrMsg());
//        }
//        log.error("{}",responseData,ex.fillInStackTrace());
//        return CommonReturnType.create(responseData, "fail");
//    }
}
